﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.UnityExtensions;
using Jbe.TestSuite.Infrastructure.Shell.Presentation;
using Microsoft.Practices.Composite.Modularity;
using System.Windows;
using Microsoft.Practices.Unity;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Windows.Controls;
using Jbe.TestSuite.Infrastructure.Shell.Applications;
using AvalonDock;

namespace Jbe.TestSuite.Infrastructure.Shell
{
    internal class Bootstrapper : UnityBootstrapper
    {
        private IApplicationContainer applicationContainer;
        private ShellPresentationModel presentationModel;
        
        protected override IModuleCatalog GetModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            applicationContainer = Container.Resolve<ApplicationContainer>();
            applicationContainer.RegisterType<IApplicationContainer, ApplicationContainer>(true);
            applicationContainer.RegisterType<IShellView, ShellWindow>(true);
            applicationContainer.RegisterType<IMessageService, MessageService>(true);
            applicationContainer.RegisterType<IShowAboutView, ShowAboutViewService>(false);
        }

        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(ToolBarTray), applicationContainer.Resolve<ToolBarTrayRegionAdapter>());
            mappings.RegisterMapping(typeof(DockablePane), applicationContainer.Resolve<DockablePaneRegionAdapter>());
            mappings.RegisterMapping(typeof(DocumentPane), applicationContainer.Resolve<DocumentPaneRegionAdapter>());
            return mappings;
        }

        protected override DependencyObject CreateShell()
        {
            presentationModel = applicationContainer.Resolve<ShellPresentationModel>();
            applicationContainer.RegisterInstance<IStatusMessageService>(presentationModel);
            presentationModel.Show();
            return presentationModel.View as DependencyObject;
        }

        public void ReLayout()
        {
            if (presentationModel != null)
            {
                presentationModel.View.ReLayout();
            }
        }
    }
}
